<?php

class EventDAL {
	private $database = NULL;

	private static $k_table = 'event';
	private static $k_start_date = 'start_date';
	private static $k_end_date = 'end_date';
	private static $k_visitor_event_id = 'visitor_event_id';
	private static $k_staff_event_id = 'staff_event_id';

	public function __construct(Database $db) {
    	$this->database = $db;
	}

	public static function getEventTable() {
		return self::$k_table;
	}

	public static function getStartDateColumn() {
		return self::$k_start_date;
	}

	public static function getVisitorEventIdColumn() {
		return self::$k_visitor_event_id;
	}

	/**
	* Spara information om event i databasen
	*/
	public function addEventMetaData(EventPair $event) {
		$start = $event->visitorEvent->getStartDateTime();
		$end = $event->visitorEvent->getEndDateTime();
		$visitorEventId = $event->visitorEvent->getId();
		$staffEventId = $event->staffEvent->getId();

		$sql = 'INSERT INTO `' . self::$k_table . '` (`' . self::$k_start_date . '`,`' . self::$k_end_date . '`, `' . self::$k_visitor_event_id . '`, `' . self::$k_staff_event_id . '`) VALUES (?, ?, ?, ?);';
		$stmt = $this->database->prepare($sql);
		$stmt->bind_param("ssss", $start, $end, $visitorEventId, $staffEventId);
		$this->database->ExecuteNonQuery($stmt);
	}

	/**
	* Uppdatera information om event i databasen
	*/
	public function updateEventMetaData(EventPair $event) {
		$start = $event->visitorEvent->getStartDateTime();
		$end = $event->visitorEvent->getEndDateTime();
		$visitorEventId = $event->visitorEvent->getId();
		$staffEventId = $event->staffEvent->getId();

		$sql = 'UPDATE `' . self::$k_table . '` SET `' . self::$k_start_date . '` = ?,`' . self::$k_end_date . '` = ?, `' . self::$k_visitor_event_id . '` = ?, `' . self::$k_staff_event_id . '` = ? WHERE `' . self::$k_visitor_event_id . '` = ?';
		$stmt = $this->database->prepare($sql);
		$stmt->bind_param("sssss", $start, $end, $visitorEventId, $staffEventId, $visitorEventId);
		$this->database->ExecuteNonQuery($stmt);
	}

	/**
	* Hämta information om event ur databasen
	*
	* @return array EventPair
	*/
	public function getEventsMetaData($visitorEventId = null) {
		$stmt = null;
		$events = array();

		if ($visitorEventId == null) {
			$sql = 'SELECT `' . self::$k_start_date . '`,`' . self::$k_end_date . '`, `' . self::$k_visitor_event_id . '`, `' . self::$k_staff_event_id . '` FROM `' . self::$k_table . '` ORDER BY `' . self::$k_start_date . '` ASC';
			$stmt = $this->database->prepare($sql);
    	} else {
			$sql = 'SELECT `' . self::$k_start_date . '`,`' . self::$k_end_date . '`, `' . self::$k_visitor_event_id . '`, `' . self::$k_staff_event_id . '` FROM `' . self::$k_table . '` WHERE `' . self::$k_visitor_event_id . '` =?';
			$stmt = $this->database->prepare($sql);
			$stmt->bind_param("s", $visitorEventId);
    	}

    	$stmt->execute();
    	$stmt->bind_result($start, $end, $visitorEventId, $staffEventId);
    	
    	while ($stmt->fetch()) {
    		$events[] = EventPair::createFromMetaData($visitorEventId, $staffEventId, $start, $end);
    	}

    	return $events;
	}

	/**
	* Ta bort information om event ur databasen
	*/
	public function deleteEventsMetaData($visitorEventId) {
		$stmt = null;
		
		$sql = 'DELETE FROM `' . self::$k_table . '` WHERE `' . self::$k_visitor_event_id . '` = ?';
		$stmt = $this->database->prepare($sql);
		$stmt->bind_param("s", $visitorEventId);
		$this->database->ExecuteNonQuery($stmt);
	}
}